package com.onlyxiahui.extend.query.hibernate.tuple;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.hibernate.tuple.AnnotationValueGeneration;
import org.hibernate.tuple.GenerationTiming;
import org.hibernate.tuple.ValueGenerator;

import com.onlyxiahui.extend.query.hibernate.annotation.CreatedDateTime;

/**
 * Description <br>
 * Date 2020-09-24 22:37:56<br>
 * 
 * @author XiaHui [onlovexiahui@qq.com]<br>
 * @since 1.0.0
 */

public class CreatedDateTimeGeneration implements AnnotationValueGeneration<CreatedDateTime> {
	private static final long serialVersionUID = 1L;

	private ValueGenerator<?> generator;
	private String FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm:ss";

	@Override
	public void initialize(CreatedDateTime annotation, Class<?> propertyType) {
		generator = (session, owner) -> DateTimeFormatter.ofPattern(FORMAT_DATE_TIME).format(LocalDateTime.now());
	}

	@Override
	public GenerationTiming getGenerationTiming() {
		return GenerationTiming.INSERT;
	}

	@Override
	public ValueGenerator<?> getValueGenerator() {
		return generator;
	}

	@Override
	public boolean referenceColumnInSql() {
		return false;
	}

	@Override
	public String getDatabaseGeneratedReferencedColumnValue() {
		return null;
	}
}
